package com.qianbei.user;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.qianbei.R;
import com.qianbei.common.base.BaseActivity;
import com.qianbei.common.init.SetData;
import com.qianbei.common.net.control.ServerControl;
import com.qianbei.common.net.control.ServerResult;
import com.qianbei.common.net.view.MsgAlert;
import com.qianbei.common.web.WebViewFragment;
import com.qianbei.share.ui.ShareFragmentDialog;
import com.umeng.socialize.UMShareAPI;

import java.util.List;

/**
 * 范例 activity
 * <p>
 * Created by user on 16/5/11.
 */
public class ExampleActivity extends BaseActivity implements ShareFragmentDialog.OnShareSuccess {

    private WebViewFragment wv;
    private String urlshare;
    private List<String> from;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gufei_erro_acitivty);

        setTitle("收徒");
        final String url = URL + "user/fenxiang/uid/" + SetData.getUserID();
        if (url != null) {
            wv = WebViewFragment.newInstance(url, false);
            showFragment(wv, R.id.contentframe);
        }
        initServer(Request.Method.GET, SHARE_WEBURL + SetData.getUserID() + "?");

        findViewById(R.id.income_bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (urlshare == null || TextUtils.isEmpty(urlshare)) {
                    new MsgAlert().show("未获取到链接，请检查网络。");
                } else {
                    ShareFragmentDialog shareFragmentDialog = ShareFragmentDialog.newInstance("快来和我一起赚零钱吧",
                            "阅读创造价值，刷不完的有趣咨询，赚不完的零钱，快戳这里！",
                            urlshare, "", "web", false);
                    shareFragmentDialog.show(getFragmentManager(), this.getClass().toString());
                    shareFragmentDialog.setOnShareSuccess(ExampleActivity.this);
                }

            }
        });
    }


    /**
     * 网络回调接口 如果一个 activity 中只有一个请求 ，回掉就用次处 ，否则自定义回掉
     */
    @Override
    public void serverFinish(ServerResult sr) {
        if (sr.isContinue) {
            ShareUrlBean shareUrlBean = JSON.parseObject(sr.bodyData.toString(), ShareUrlBean.class);

            urlshare = shareUrlBean.getData().getUrl();
            from = shareUrlBean.getData().getFrom();

        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean OnShareSuccess(boolean isShare) {
        if (isShare) {
            ServerControl sc = new ServerControl(Request.Method.GET, SHARE_FRIENDS + SetData.getUserID() + "?");

            sc.serverListener = new ServerControl.ServerListener() {
                @Override
                public void serverFinish(ServerResult sr) {
                    if (sr.isContinue) {
                        //无提示
                    }
                }
            };
            sc.startVolley();
        }
        return false;
    }
}
